package com.bootssm.controller;

import com.bootssm.entity.PageView;
import com.bootssm.entity.Result;
import com.bootssm.entity.Student;
import com.bootssm.entity.StudentVO;
import com.bootssm.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @PostMapping("/findAllStudent")
    public PageView<Student> findAll(@RequestBody StudentVO studentVO){

        Integer page=studentVO.getPage();
        Integer start=(page-1)*studentVO.getSize();
        studentVO.setStart(start);
        List<Student> studentList= studentService.findAll(studentVO);
        Integer count=studentService.getCount(studentVO);
        PageView<Student> pageView = new PageView<>();
        pageView.setDataList(studentList);
        pageView.setCount(count);
        return pageView;

    }
    @PostMapping("/deleteStudentById")
    public Result deleteById(@RequestBody Student student){
        String id= student.getId();
        studentService.deleteById(id);
        return new Result("200","删除成功");
    }
    @PostMapping("/updateStudent")
    public Result update(@RequestBody Student student){
        try{
            studentService.update(student);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","修改失败");
        }
        return new Result("200","修改成功");
    }
    @PostMapping("/saveStudent")
    public Result save(@RequestBody Student student){

        try{
            studentService.save(student);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","保存失败");
        }
        return new Result("200","保存成功");
    }
}
